package com.unknown.common.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public abstract class CommonRecyclerViewAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {
    private Context context;
    private List<T> data;
    private int layoutResource;

    protected CommonRecyclerViewAdapter(Context context, int layoutResource) {
        this.context = context;
        this.data = new ArrayList<>();
        this.layoutResource = layoutResource;
    }

    @NonNull
    @Override
    public CommonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CommonViewHolder(LayoutInflater.from(context).inflate(layoutResource, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CommonViewHolder holder, int position) {
        bind(holder, data.get(position), null);
    }

    @Override
    public void onBindViewHolder(@NonNull CommonViewHolder holder, int position, @NonNull List<Object> payloads) {
        bind(holder, data.get(position), payloads);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public abstract void bind(CommonViewHolder commonViewHolder, T item, List<Object> payloads);

    public void setData(List<T> data) {
        this.data = data;
    }

    public void bindRecyclerView(RecyclerView recyclerView) {
        recyclerView.setItemAnimator(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(this);
    }

//    public void bindRecyclerView(RecyclerView recyclerView, RecyclerView.LayoutManager layoutManager) {
//        recyclerView.setItemAnimator(null);
//        recyclerView.setLayoutManager(layoutManager);
//        recyclerView.setAdapter(this);
//    }
}
